package com.example.gzysmartcity.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ThreadUtils;
import com.example.gzysmartcity.repository.model.Reserve;
import com.example.gzysmartcity.repository.model.SCDoctor;
import com.example.gzysmartcity.repository.model.SCVisit;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.utils.NetworkUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class DoctorViewModel extends ViewModel {
    MutableLiveData<State> doctorLiveData=new MutableLiveData<>();
    List<SCDoctor.DataBean> SCDoctorList;
    List<SCVisit.DataBean> SCVisitList;
    List<Reserve> Reserve;
    int mzId;


    public void getSCVisitData(){
       ThreadUtils.executeByCached(new ThreadUtils.Task<SCVisit>() {
           @Override
           public SCVisit doInBackground() throws Throwable {
               return NetworkUtil.getInstance().get("SCVisit/getAll?", null, SCVisit.class, 0);
           }

           @Override
           public void onSuccess(SCVisit result) {
               SCVisitList=result.getData().stream().filter(a -> a.getSCOutpatientId() == mzId).collect(Collectors.toList());
               getSCDoctorData();
           }

           @Override
           public void onCancel() {

           }

           @Override
           public void onFail(Throwable t) {

           }
       });
   }

   public void getSCDoctorData(){
       ThreadUtils.executeByCached(new ThreadUtils.Task<SCDoctor>() {
           @Override
           public SCDoctor doInBackground() throws Throwable {
               return NetworkUtil.getInstance().get("SCDoctor/getAll?", null, SCDoctor.class, 0);
           }

           @Override
           public void onSuccess(SCDoctor result) {
               SCDoctorList=result.getData().stream().filter(a -> a.getSCOutpatientId() == mzId).collect(Collectors.toList());
               getDoctor();

           }

           @Override
           public void onCancel() {

           }

           @Override
           public void onFail(Throwable t) {

           }
       });
   }
   public void getDoctor(){
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
            @Override
            public Object doInBackground() {
                Reserve=new ArrayList<>();
                for (SCDoctor.DataBean dataDTO : SCDoctorList) {
                    Reserve.add(new Reserve(SCVisitList.stream().filter(a -> a.getSCDoctorId() == dataDTO.getId()).sorted(Comparator.comparing(SCVisit.DataBean::getTreatmentDate)).collect(Collectors.toList()), dataDTO));
                }
                return null;
            }

            @Override
            public void onSuccess(Object result) {
                doctorLiveData.postValue(State.SUCCESS);
            }
        });
   }
   public void getMzId(int id){
        mzId=id;
   }

    public LiveData<State> getDoctorLiveData(){
        return doctorLiveData;
    }

    public List<SCDoctor.DataBean> getSCDoctorList() {
        return SCDoctorList;
    }

    public List<SCVisit.DataBean> getSCVisitList() {
        return SCVisitList;
    }

    public List<Reserve> getReserve() {
        return Reserve;
    }
}
